package com.sh.personnel.controller.sys;

import com.github.pagehelper.PageInfo;
import com.sh.personnel.domain.ITbUser;
import com.sh.personnel.service.IUserService;
import com.sh.personnel.util.result.Result;
import com.sh.personnel.vo.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户控制
 * @author 林思浩
 * @date 2020/03/12 15:48
 */
@RestController()
@RequestMapping("/user")
public class IUserController {

    @Autowired
    IUserService userService;

    //分页获取所有用户列表
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:user:view')")
    public Result getUserAllList(@RequestParam("page") Integer page,
                                 @RequestParam(required = false, value = "key") String key,
                                 @RequestParam(required = false, value = "deptid") String deptId,
                                 @RequestParam(required = false, value = "state") Integer state) {
        PageInfo<UserDto> userList = userService.getUserList(page, key, deptId, state);
        return Result.success().setData("user_list", userList);
    }

    //添加用户
    @PostMapping()
    @PreAuthorize("hasAuthority('sys:user:insert')")
    public Result addUser(@RequestBody ITbUser user) {
        System.out.println(user.toString());

        boolean bool = userService.insertUser(user);
        return bool ? Result.success() : Result.fail();
    }

    //获取用户信息
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result getUserByUsername(@PathVariable String id) {
        ITbUser user = userService.getUserById(id);
        return Result.success().setData("user", user);
    }

    //更新用户信息
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result alterUser(@RequestBody ITbUser user) {
        System.out.println(user);
        boolean bool = userService.updateUserById(user);
        return bool ? Result.success("更新成功") : Result.fail("更新失败");
    }

    //删除用户
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result deleteUser(@PathVariable String id) {
        boolean bool = userService.deleteUserById(id);
        return bool ? Result.success() : Result.fail();
    }
}
